﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;

namespace MovieTweet
{
    public class TwitterResult
    {
        public string created_at;
        public string from_user;
        public string text;
    }

    public class TwitterJsonObject
    {
        public string next_page;
        public List<TwitterResult> results;
    }

    public class TwitterInfo
    {
        public static string baseUrl = "http://search.twitter.com/search.json";

        public TwitterJsonObject getJson(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String jsonData = reader.ReadToEnd();

            JavaScriptSerializer jSerialize = new JavaScriptSerializer();
            TwitterJsonObject twitterJsonObject = jSerialize.Deserialize<TwitterJsonObject>(jsonData);

            return twitterJsonObject;
        }


        public List<TwitterResult> getLatest(string movie, int results)
        {
            string url = baseUrl + "?rpp=" + results;
			url += "&since=" + DateTime.Now.Date.ToString("yyyy-MM-dd");
			//url += "&since=" + DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");
            url += "&q=" + HttpUtility.UrlEncode("\"" + movie + "\"" + "movie OR film");
			List<TwitterResult> twitterResults = new List<TwitterResult>();

            TwitterJsonObject twitterJsonObject = getJson(url);

            foreach (TwitterResult result in twitterJsonObject.results)
            {
				result.created_at = result.created_at.Substring(17, 5);
				twitterResults.Add(result);
            }
            return twitterResults;
        }

        public int countMentions(string movie)
        {
            string url = baseUrl + "?rpp=100";
			url += "&since=" + DateTime.Now.Date.ToString("yyyy-MM-dd");
			//url += "&since=" + DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");
            url += "&q=" + HttpUtility.UrlEncode("\"" + movie + "\"" + "movie OR film");

            TwitterJsonObject twitterJsonObject = getJson(url);

            int mentions = 0;
            while (true)
            {
                if (twitterJsonObject.next_page != null)
                {
                    twitterJsonObject = getJson(baseUrl + twitterJsonObject.next_page);
                    mentions += 100;
                }
                else
                {
                    mentions += twitterJsonObject.results.Count;
                    break;
                }
            }

            return mentions;
        }

        //static void Main(string[] args)
        //{
        //    Console.WriteLine("Mentions: " + countMentions("Dolphin tale"));
        //    Console.WriteLine("Latest 5 mentions:");
        //    Console.WriteLine();
        //    printLatest("Dolphin Tale", 5);
        //}
    }
}